package com.soulkun.smarthome.ui.main.home

import android.Manifest
import android.view.View
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainHomeRoomBinding
import com.soulkun.smarthome.databinding.AppRecycleItemMainHomeDeviceListBinding
import com.soulkun.smarthome.logic.Constant
import com.soulkun.smarthome.ui.main.home.adapter.AppMainHomeDeviceListAdapter
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeViewModel
import soulkun.library.helper.decoration.LinearSpaceItemDecorationHelper
import soulkun.library.mvvm.component.BaseDataBindingFragment
import soulkun.library.utils.permission.PermissionUtils

class AppMainHomeRoomFragment(private val position: Int, private val fragment: AppMainHomeFragment, private val viewModel: AppMainHomeViewModel) :
    BaseDataBindingFragment<AppFragmentMainHomeRoomBinding>() {

    private val adapter by lazy {
        AppMainHomeDeviceListAdapter(mContext, viewModel.roomDeviceList[position].deviceList){ binding ->
            View.OnClickListener {
                viewModel.selectDeviceLiveData.value = (binding as AppRecycleItemMainHomeDeviceListBinding).deviceInfoVO!!
                findNavController().navigate(R.id.appMainHomeDeviceFragment)
            }
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_home_room
    }

    override fun initView() {
        requireDataBinding().rvMainHomeRoomListDevice.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = this@AppMainHomeRoomFragment.adapter
            if (itemDecorationCount == 0) {
                addItemDecoration(LinearSpaceItemDecorationHelper(10, 10, 15, 15, 10))
            }
        }
    }

    override fun initData() {
        requireDataBinding().arrayList = viewModel.roomDeviceList[position].deviceList
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.btn_main_home_room_list_empty_text_add_device -> {
                    if (PermissionUtils.fragmentCheckPermissionList(
                            fragment,
                            mContext,
                            listOf(Manifest.permission.CAMERA),
                            Constant.PERMISSION_REQUEST_CAMERA
                        )
                    ) {
                        fragment.startAddDeviceCamera()
                    }
                }
            }
        }
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

}